package com.century.sjt.EMChat.widget.chatrow;

import android.content.Context;
import android.text.Spannable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.century.sjt.EMChat.utils.EaseSmileUtils;
import com.century.sjt.R;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;
import com.easemob.exceptions.EaseMobException;

/**
 * Created by asus on 2016/9/19.
 */
public class MyChatRowTextShare extends EaseChatRow {
    private TextView contentView;
    private ImageView iv_img;
    private TextView tVPrice;

    public MyChatRowTextShare(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_share_message : R.layout.ease_row_sent_share_message, this);
    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
        tVPrice = (TextView) findViewById(R.id.price);
        iv_img = (ImageView) findViewById(R.id.iv_img);
    }

    @Override
    public void onSetUpView() {
//        String Translate = message.getStringAttribute("Translate",null);
        String mainImageUrl = message.getStringAttribute("mainImageUrl",null);
        String price = message.getStringAttribute("price",null);
        TextMessageBody txtBody = (TextMessageBody) message.getBody();

        if ("".equals(mainImageUrl)||null==mainImageUrl){
            Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
            // 设置内容
            contentView.setText(span, TextView.BufferType.SPANNABLE);
        }else {
            Glide.with(context).load(mainImageUrl).into(iv_img);
            tVPrice.setText(price);
            Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
            // 设置内容
            contentView.setText(span, TextView.BufferType.SPANNABLE);
        }


        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (message.direct == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status) {
                case CREATE:
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    // 发送消息
//                sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }else{
            if(!message.isAcked() && message.getChatType() == EMMessage.ChatType.Chat){
                try {
                    EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
                    message.isAcked = true;
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub

    }
}
